<!-- 发布消息页面
codeslizhi
@email: 1664234934@qq.com
@Date: 2022 3 9
 -->
<template>
	<view>
		<cu-custom class="bg-gradual-blue" :isBack="false">
			<block slot="content">发布消息</block>
		</cu-custom>
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#0081ff" ref="tabs" :list="list" :current="current" @change="change"
					:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition"
				@animationfinish="animationfinish">

				<!-- 第一页 -->
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<form>
								<view class="cu-form-group">
									<view class="title">标题：</view>
									<input placeholder="请输入标题" name="input" v-model="info1.title"></input>
								</view>
								<!-- <view class="cu-form-group">
									<view class="title">联系方式：</view>
									<input placeholder="输入手机号码" name="input" v-model="info1.mobile"></input>
									<view class="cu-capsule radius">
										<view class='cu-tag bg-blue '>
											+86
										</view>
										<view class="cu-tag line-blue">
											中国大陆
										</view>
									</view>
								</view> -->
								<view class="cu-form-group margin-top">
									<view class="title">物品的类别</view>
									<picker @change="PickerChange" :value="index" :range="picker">
										<view class="picker">
											{{index>-1?picker[index]:'请选择物品的类别'}}
										</view>
									</picker>
								</view>
								
								<view class="cu-form-group margin-top">
									<textarea style="height: 250rpx;" placeholder="请输入物品描述" name="input" v-model="info1.about"></textarea>
								</view>
								<view class="cu-bar bg-white margin-top">
									<view class="action">
										物品图片
									</view>
									<view class="action">
										{{imgList.img_url}}
									</view>
								</view>
								<view class="cu-form-group">
									<view class="grid col-4 grid-square flex-sub">
										<view class="bg-img" v-for="(item,index) in imgList" :key="index"
											@tap="ViewImage" :data-url="imgList[index]">
											<image :src="imgList[index]" mode="aspectFill"></image>
											<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
												<text class='cuIcon-close'></text>
											</view>
										</view>
										<view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
											<text class='cuIcon-cameraadd'></text>
										</view>
									</view>
								</view>
								<view class="padding flex flex-direction">
									<button class="cu-btn bg-blue lg" @click="onSubmit">提交</button>
								</view>
							</form>
						</view>
					</scroll-view>
				</swiper-item>


				<!-- 第二页 -->
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<form>
								<view class="cu-form-group">
									<view class="title">标题：</view> 
									<input placeholder="请输入拾物人姓名" name="input" v-model="info2.title"></input>
								</view>
								<!-- <view class="cu-form-group">
									<view class="title">拾物人联系方式：</view>
									<input placeholder="输入手机号码" name="input" v-model="info2.flContact"></input>
									<view class="cu-capsule radius">
										<view class='cu-tag bg-blue '>
											+86
										</view>
										<view class="cu-tag line-blue">
											中国大陆
										</view>
									</view>
								</view> -->

								<view class="cu-form-group margin-top">
									<view class="title">物品的类别</view>
									<picker @change="PickerChange2" :value="index" :range="picker"> 
										<view class="picker"> 
											{{index>-1?picker[index]:'请选择物品的类别'}} 
										</view>
									</picker>
								</view>
													
								<view class="cu-form-group margin-top">
									<textarea style="height: 250rpx;" placeholder="请输入物品描述" name="input" v-model="info2.about"></textarea>
								</view>
					
								<view class="cu-bar bg-white margin-top">
									<view class="action">
										物品图片
									</view>
									<view class="action">
										{{imgList.img_url}}
									</view>
								</view>
								<view class="cu-form-group">
									<view class="grid col-4 grid-square flex-sub">
										<view class="bg-img" v-for="(item,index) in imgList2" :key="index"
											@tap="ViewImage2" :data-url="imgList2[index]">
											<image :src="imgList2[index]" mode="aspectFill"></image>
											<view class="cu-tag bg-red" @tap.stop="DelImg2" :data-index="index">
												<text class='cuIcon-close'></text>
											</view>
										</view>
										<view class="solids" @tap="ChooseImage2" v-if="imgList2.length<1">
											<text class='cuIcon-cameraadd'></text>
										</view>
									</view>
								</view>
								<view class="padding flex flex-direction">
									<button class="cu-btn bg-blue lg" @click="onSubmit2">提交</button>
								</view>
							</form>
						</view>
					</scroll-view>
				</swiper-item>


			</swiper>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				index: 1,
				index2:2,
				imgList: [],
				imgList2: [],
				picker: ['', '日用类', '证件类', '现金类', '电子类', '数码类'],
				picker1: ['', '日用类', '证件类', '现金类', '电子类', '数码类'],
				percent: 50,
				activeColor: '#0081ff',
				striped: false,
				stripedActive: false,
				list: [{
						name: '寻失物'
					},
					{
						name: '寻失主'
					}
				],
				current: 0,
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				info1: {
					type:"1",//订单类型, 1找物品，2找失主
					// mobile:'17688806952',//手机号码  
					title: '',//标题
					img_url: [],//图片url数组  
					category_id:"1",//分类id   
					desc:'',//物品描述      
					about:'',//留言
				}, 
				info2: {
					type:"2",//订单类型, 1找物品，2找失主
					// mobile:'17688806952',//手机号码  
					title: '',//标题
					img_url: [],//图片url数组  
					category_id:"1",//分类id   
					desc:'',//物品描述  
					about:'',//留言 
				}
			};
		},
		onLoad() {

		},
		computed: {},
		methods: {
			ChooseImage() {
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}

						uni.uploadFile({
							url: this.$BASE_URL.BASE_URL + ':8020/oss/v1/oss/upload', 
							method: 'POST',
							name: 'file',
							filePath: res.tempFilePaths[0]
						}).then((res) => {
							console.log("res1")
							this.loading = false;
							if (res[1].statusCode === 200) { // 获取数据成功  
								var dataurl = res[1].data    
								// console.log(JSON.parse(dataurl).data.url)   
								let Arrayurl = JSON.parse(dataurl).data.url 
								this.info1.img_url.push(Arrayurl);   
								// console.log(this.info1.img_url);    
								this.$tip.success('上传成功!')
							} else if (res[1].statusCode === 500) { // 获取数据失败
								console.log("失败")
								this.loading = false;
								this.$tip.alert(res.data.message);
							}
						}).catch((err) => {
							// let msg = "请求出现错误，请稍后再试"
							// this.loading = false;
							// this.$tip.alert(msg);
						}).finally(() => {
							this.loading = false;
						})


					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '主人',
					content: '确定要删除这张照片吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			// 下拉框
			PickerChange(e) {
				this.index = e.detail.value
				this.info1.category_id = this.index
				this.info1.gid = e.detail.value
			},
			// 下拉框2
			PickerChange2(e) {
				this.index2 = e.detail.value
				this.info2.category_id = this.index
				// this.info1.gid = e.detail.value
			},
			// tab栏切换
			change(index) {
				this.swiperCurrent = index;
				// this.getOrderList(index);
			},
			transition({
				detail: {
					dx
				}
			}) {
				this.$refs.tabs.setDx(dx);
			},
			animationfinish({
				detail: {
					current
				}
			}) {
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			},
			onSubmit() {
				let myForm = this.info1
				let checkPhone = new RegExp(/^[1]([3-9])[0-9]{9}$/);
				console.log("myForm", myForm)
				console.log(myForm.title);
				if (!myForm.title || myForm.title.length == 0) {
					this.$tip.alert('请输入标题');
					return false
				}
				// if (!checkPhone.test(myForm.mobile)) {
				// 	this.$tip.alert('请输入正确的手机号');
				// 	return false
				// }
				if (!myForm.category_id || myForm.category_id.length == 0) {
					this.$tip.alert('请选择类别');
					return false
				}
				if (!myForm.about || myForm.about.length == 0) {
					this.$tip.alert('请输入物品描述');
					return false
				}
				// if (myForm.img_url.length == 0) { 
				// 	this.$tip.alert('请上传图片');
				// 	return false
				// } 
			
				// this.info1.status = '未找到'
				const _this = this // 获取此时的this为一个常量，防止下面请求回调改变出错
				console.log("表单提交") 
				// console.log(this.info1) 
				let opts = {
					url: ':8022/o/v1/order',  
					method: 'POST', 
				};  
				
				this.info1.category_id = this.info1.category_id + ""
				this.$httpTokenRequest( opts,this.info1).then((res) => {
					console.log(res) 
					this.loading = false;
					if (res.data.msg == "发布成功") { // 获取数据成功 
						console.log("成功")
						uni.switchTab({
							url: '../index/index'
						})
						// _this.info1.img_url = [];
						_this.info1.title = '';
						_this.info1.category_id = "1";
						_this.info1.desc = "";
						_this.info1.about = "";
						_this.imgList = []; 
						this.$tip.success('发布成功！')
						
					} else if (res.data.code === 500) { // 获取数据失败
						console.log("失败") 
						console.log(res)
						this.loading = false;
						this.$tip.alert(res.data.message);
					}
				}).catch((err) => {
					let msg = "请求出现错误，请稍后再试"
					this.loading = false;
					this.$tip.alert(msg);
				}).finally(() => {
					this.loading = false;
				})
			},
			ChooseImage2() {
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList2.length != 0) {
							this.imgList2 = this.imgList2.concat(res.tempFilePaths)
						} else {
							this.imgList2 = res.tempFilePaths
						}
			
						uni.uploadFile({
							url: this.$BASE_URL.BASE_URL + ':8020/oss/v1/oss/upload',
							method: 'POST',
							name: 'file',
							filePath: res.tempFilePaths[0]
						}).then((res) => {
							console.log("res1")
							console.log(res)
							this.loading = false;
							if (res[1].statusCode === 200) { // 获取数据成功
								var dataurl = res[1].data    
								console.log(JSON.parse(dataurl).data.url)   
								let Arrayurl = JSON.parse(dataurl).data.url 
								this.info2.img_url.push(Arrayurl);    
								console.log(this.info2.img_url);   
								this.$tip.success('上传成功!')
							} else if (res[1].statusCode === 500) { // 获取数据失败
								console.log("失败")
								this.loading = false;
								this.$tip.alert(res.data.message);
							}
						}).catch((err) => {
							// let msg = "请求出现错误，请稍后再试"
							// this.loading = false;
							// this.$tip.alert(msg);
						}).finally(() => {
							this.loading = false;
						})
			
			
					}
				});
			},
			ViewImage2(e) {
				uni.previewImage({
					urls: this.imgList2,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg2(e) {
				uni.showModal({
					title: '主人',
					content: '确定要删除这张照片吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList2.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			onSubmit2() {
				let myForm = this.info2
				let checkPhone = new RegExp(/^[1]([3-9])[0-9]{9}$/);
				console.log("myForm", myForm)
				// console.log(myForm.title);
				if (!myForm.title || myForm.title.length == 0) {
					this.$tip.alert('请输入标题');
					return false
				}
				// if (!checkPhone.test(myForm.mobile)) {
				// 	this.$tip.alert('请输入正确的手机号');
				// 	return false
				// }
				if (!myForm.category_id || myForm.category_id.length == 0) {
					this.$tip.alert('请选择类别');
					return false
				}
				if (!myForm.about || myForm.about.length == 0) {
					this.$tip.alert('请输入物品描述');
					return false
				}
				// this.info2.flStatus = '待招领'
			
				const _this = this // 获取此时的this为一个常量，防止下面请求回调改变出错
				console.log("表单提交")
				console.log(this.info2)
			
				let opts = {
					url: ':8022/o/v1/order',  
					method: 'POST', 
				};   

				this.info2.category_id = this.info2	.category_id + ""
				this.$httpTokenRequest( opts,this.info2).then((res) => {
					console.log(res)
					this.loading = false;
					if (res.data.msg == "发布成功") { // 获取数据成功 
						console.log("成功") 
						uni.switchTab({ 
							url: '../index/index'
						})
						_this.info2.title = ''; 
						_this.info2.category_id = "1";
						_this.info2.desc = "";
						_this.info2.about = "";
						_this.imgList2 = []; 
						this.$tip.success('发布成功！')
					} else if (res.data.code === 500) { // 获取数据失败
						console.log("失败") 
						console.log(res)
						this.loading = false;
						this.$tip.alert(res.data.message);
					}
				}).catch((err) => {
					let msg = "请求出现错误，请稍后再试"
					this.loading = false;
					this.$tip.alert(msg);
				}).finally(() => {
					this.loading = false;
				})
			},
			
		}
	};
</script>

<style lang="scss" scoped>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss" scoped>
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 34rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;

				.progressBox {
					width: 150rpx;
					float: right;
				}
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 260rpx;
					height: 190rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 45rpx;
				}

				.type {
					margin: 6rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
					text-overflow: -o-ellipsis-lastline;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 3;
					line-clamp: 3;
					-webkit-box-orient: vertical;
				}

				.delivery-time {
					color: #0081ff;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			line-height: 70rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.btnBox {
				width: 150rpx;
				display: flex;
				justify-content: space-between;

				.btn {
					line-height: 52rpx;
					width: 140rpx;
					border-radius: 12rpx;
					border: 2rpx solid $u-tips-color;
					font-size: 26rpx;
					text-align: center;
					color: $u-tips-color;
				}

				.evaluate {
					color: $u-type-primary;
					border-color: $u-type-primary;
				}
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 300rpx;
			border-radius: 50%;
			margin: 0 auto;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: #ffd23c
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
	.bg-gradual-blue {
		background-image: linear-gradient(45deg, #0081ff, #1cbbb4) !important;
		color: #ffffff; 
	}
</style>
